﻿using System;
using System.IO;
using System.Reflection;
using System.Windows;
using Alfa.Common.UI.Dialogs;
using NotifyIcon = System.Windows.Forms.NotifyIcon;

namespace JBot {
	/// <summary>
	/// Interaction logic for App.xaml
	/// </summary>
	public partial class App : Application {
		public static JClient.ConnectionProvider xmpp {
			get {
				if (App.Current.Resources.Contains("xmpp")) {
					var dataCenter = (JClient.ConnectionProvider)App.Current.Resources["xmpp"];
					return dataCenter;
				}
				return null;
			}
		}

		public static NotifyIcon NotifyIcon {
			get;
			private set;
		}

		public App( ) {
			NotifyIcon = new NotifyIcon( );
			NotifyIcon.Icon = JBot.Properties.Resources.JBot;
			NotifyIcon.BalloonTipText = AppInfo.Title;
			this.DispatcherUnhandledException += App_DispatcherUnhandledException;
		}

		private void App_Exit(object sender, ExitEventArgs e) {
			App.NotifyIcon.Visible = false;
		}

		private void App_DispatcherUnhandledException(object sender, System.Windows.Threading.DispatcherUnhandledExceptionEventArgs e) {
			App.NotifyIcon.Visible = false;
			//Environment.Exit(System.Runtime.InteropServices.Marshal.GetHRForException(e.Exception));
			//e.Handled = true;
			ErrorMessageWindow errorWindow = new ErrorMessageWindow(e.Exception);
			errorWindow.ShowDialog( );
			//Exception ex = e.Exception;

			//MessageBox.Show("Whoops! Please contact the developers with the following"
			//	  + " information:\n\n" + ex.Message + ex.StackTrace,
			//	  "Fatal Error", MessageBoxButton.OK, MessageBoxImage.Stop);
			e.Handled = true;
			App.Current.Shutdown( );
		}
	}
}
